package main.java.main.util;


import java.io.*;
import java.util.*;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import main.java.main.bean.AutoBean;
import main.java.main.bean.CoverageBean;
import main.java.main.bean.Method;
import main.java.main.util.unitObj.AutowiredObj;
import main.java.main.util.unitObj.MethodObj;

public class CreateUtil {

    public static boolean isMethodFlag = false;

    public static String DateTimeInstance() {

        return "";
    }

    /**
     * 创建API目录代码
     *
     * @param bean
     * @throws Exception
     */
    public static void createSrvCoverageCode(CoverageBean bean) throws Exception {

        //**********************************生成目录**********************************************************//
        // 模板生成后存放目录
//        String targetPath = bean.getAutoPath();
        String targetPath = bean.getAutoPath();
        //创建输入模块主目录
        targetPath = targetPath + "/" + bean.getFunctionId() + "Corerage代码";
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/srv";
        CreateFilePath(apiMk);
        //生成子模块目录 /t0101
//        String subMoPath = apiMk + "/t" + bean.getNumMain() + bean.getNumSub();
        String subMoPath = apiMk + "/" + bean.getFunctionId();
        String subMoImplPath = subMoPath + "/impl";
        String subMoCtrlPath = subMoImplPath + "/ctrl";
        String subMoDaoPath = subMoImplPath + "/dao";
        String subMoServPath = subMoImplPath + "/service";

        CreateFilePath(subMoPath);
        CreateFilePath(subMoImplPath);
        CreateFilePath(subMoCtrlPath);
        CreateFilePath(subMoDaoPath);
        CreateFilePath(subMoServPath);
        //*************************************生成Dao 4文件*******************************************************//

        //创建 BaseAreaServiceImpl.java
//        String daoFileName = getKey("JAVA_HEADER + "ServiceImpl.java";
        File dir = new File(bean.getFromPath());
        findFile(dir, subMoPath, bean);

        //生成common;
        String subMoCommonPath = subMoPath + "/common";
        CreateFilePath(subMoCommonPath);
        String subMoCommonCtrlPath = subMoCommonPath + "/ctrl";
        CreateFilePath(subMoCommonCtrlPath);
        String subMoServicePath = subMoCommonPath + "/service";
        CreateFilePath(subMoServicePath);
        String subMoCommonDaoPath = subMoCommonPath + "/dao";
        CreateFilePath(subMoCommonDaoPath);
        String[] daoTemNames = {"CommonCtrl", "CommonDao", "CommonService"};
        String[] daoTemPaths = {"ctrl", "dao", "service"};
        String[] daoFile = {
                subMoCommonCtrlPath + "/" + "CommonCtrl.java",

                subMoCommonDaoPath + "/" + "CommonDao.java",
                subMoServicePath + "/" + "CommonService.java",
        };
        // 生成目标文件
        String daoPath = "V6NewTools/src/main/resources/templates" + "/coverage/common/";

        //对bean处理
//        String classPath = "com.flux.tms.tm"+bean.getNumMain()+"srv.t"+bean.getNumMain()+bean.getNumSub()+".impl";
//        bean = MockTest.GetMockFunction(classPath,bean);
        for (int i = 0; i < daoTemNames.length; i++) {
            System.out.println(daoFile[i]);
            bean.setFileName(daoTemNames[i]);
            WriteTestFile(daoFile[i], bean, daoPath + daoTemPaths[i], daoTemNames[i] + ".ftl");
        }

//        String[] daoTemNames = {"base.ftl", "add.ftl", "del.ftl", "update.ftl"};
//        String[] daoFile = {
//                subMoDaoPath + "/" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Add" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Delete" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Update" + bean.getJavaFlieName() + "Dao.java",
//        };
//        // 生成目标文件
//        String daoPath = bean.getTemplateDirectory() + "/srv/impl/dao";
//        for (int i = 0; i < daoTemNames.length; i++) {
//            System.out.println(daoFile[i]);
//            WriteFile(daoFile[i], bean, daoPath, daoTemNames[i]);
//        }


        //*********************************************************************************************************//
    }


    /**
     * 创建API目录代码
     *
     * @param bean
     * @throws Exception
     */
    public static void createWebCoverageCode(CoverageBean bean) throws Exception {

        //**********************************生成目录**********************************************************//
        // 模板生成后存放目录
//        String targetPath = bean.getAutoPath();
        String targetPath = bean.getAutoPath();
        //创建输入模块主目录
        targetPath = targetPath + "/" + bean.getFunctionId() + "Corerage代码";
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/web";
        CreateFilePath(apiMk);
        //生成子模块目录 /t0101
//        String subMoPath = apiMk + "/t" + bean.getNumMain() + bean.getNumSub();
        String subMoPath = apiMk + "/" + bean.getFunctionId();
        String subMoImplPath = subMoPath + "";
//        String subMoCtrlPath = subMoImplPath + "/ctrl";
        String subMoDaoPath = subMoImplPath + "/dao";
        String subMoServPath = subMoImplPath + "/service";

        CreateFilePath(subMoPath);
        CreateFilePath(subMoImplPath);
//        CreateFilePath(subMoCtrlPath);
        CreateFilePath(subMoDaoPath);
        CreateFilePath(subMoServPath);
        //*************************************生成Dao 4文件*******************************************************//

        //创建 BaseAreaServiceImpl.java
//        String daoFileName = getKey("JAVA_HEADER + "ServiceImpl.java";
        File dir = new File(bean.getFromPath());
        findFile(dir, subMoPath, bean);

        //生成common;
        String subMoCommonPath = subMoPath + "/common";
        CreateFilePath(subMoCommonPath);
//        String subMoCommonCtrlPath = subMoCommonPath + "/ctrl";
//        CreateFilePath(subMoCommonCtrlPath);
        String subMoServicePath = subMoCommonPath + "/service";
        CreateFilePath(subMoServicePath);
        String subMoCommonDaoPath = subMoCommonPath + "/dao";
        CreateFilePath(subMoCommonDaoPath);
        String[] daoTemNames = { "CommonDao", "CommonService"};
        String[] daoTemPaths = {"dao", "service"};
        String[] daoFile = {
//                subMoCommonCtrlPath + "/" + "CommonCtrl.java",

                subMoCommonDaoPath + "/" + "CommonDao.java",
                subMoServicePath + "/" + "CommonService.java",
        };
        // 生成目标文件
        String daoPath = "V6NewTools/src/main/resources/templates" + "/coverage/common/";

        //对bean处理
//        String classPath = "com.flux.tms.tm"+bean.getNumMain()+"srv.t"+bean.getNumMain()+bean.getNumSub()+".impl";
//        bean = MockTest.GetMockFunction(classPath,bean);
        for (int i = 0; i < daoTemNames.length; i++) {
            System.out.println(daoFile[i]);
            bean.setFileName(daoTemNames[i]);
            WriteTestFile(daoFile[i], bean, daoPath + daoTemPaths[i], daoTemNames[i] + ".ftl");
        }

//        String[] daoTemNames = {"base.ftl", "add.ftl", "del.ftl", "update.ftl"};
//        String[] daoFile = {
//                subMoDaoPath + "/" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Add" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Delete" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Update" + bean.getJavaFlieName() + "Dao.java",
//        };
//        // 生成目标文件
//        String daoPath = bean.getTemplateDirectory() + "/srv/impl/dao";
//        for (int i = 0; i < daoTemNames.length; i++) {
//            System.out.println(daoFile[i]);
//            WriteFile(daoFile[i], bean, daoPath, daoTemNames[i]);
//        }


        //*********************************************************************************************************//
    }

    public static String findFile(File dir, String subMoPath, CoverageBean bean) throws Exception {
        if (dir.isDirectory()) {
            File[] subs = dir.listFiles();
            if (subs != null) {
                for (File sub : subs) {
                    findFile(sub, subMoPath, bean);//递归，如果获取文件夹的话，走里面的文件
                }
            }
        }
        if (dir.isFile()) {//如果dir是文件名
            String str = dir.getName();
            if (str.endsWith(".java") && !str.endsWith("CVal.java") && !str.endsWith("Impl.java")) {
                //读取到的目录
                String path = dir.getAbsolutePath();
                String fileName = dir.getName();
//                System.out.println(fileName);
//                System.out.println(path);

                readStr(dir, subMoPath, bean);
            }
        }
        return "";
    }


    public static void readStr(File dir, String path, CoverageBean bean) {
        String daoPath = "V6NewTools/src/main/resources/templates" + "/coverage/base/";
        String fileName = dir.getName();
        bean.setFileName(fileName.replace(".java", ""));
        bean.setPk05(fileName.replace(".java", "")
                .replace("Add", "")
                .replace("Update", "")
                .replace("Delete", "")
                .replace("Del", "")
                .replace("Ctrl", "")
                .replace("Dao", "")
                .replace("Service", ""));
        //写入组织数据；
        String writeFiePath = null;//要写入的文件
        String ftlPath = null;//模板文件
        String ftlName = null;
        if (fileName.endsWith("Ctrl.java")) {
            bean.setserviceName(fileName.replace(".java", "").replace("Ctrl", ""));
            System.out.println(bean.getserviceName());
            writeFiePath = path + "/impl/ctrl/" + fileName.replace(".java", "Test.java");
            ftlPath = daoPath + "ctrl";
            ftlName = "AddServiceCtrlTest.ftl";
        } else if (fileName.endsWith("Service.java")) {
            String daoName = fileName.replace(".java", "").replace("Service", "Dao");
            daoName = (new StringBuilder()).append(Character.toLowerCase(daoName.charAt(0))).append(daoName.substring(1)).toString();
            bean.setdaoName(daoName);

            writeFiePath = path + "/impl/service/" + fileName.replace(".java", "Test.java");
            ftlPath = daoPath + "service";
            ftlName = "AddServiceTest.ftl";
        } else if (fileName.endsWith("Dao.java")) {
//            map.put("DaoName",fileName.replace(".java","").replace("Service","Dao"));

            writeFiePath = path + "/impl/dao/" + fileName.replace(".java", "Test.java");
            ftlPath = daoPath + "dao";
            ftlName = "AddDaoTest.ftl";
        }

        //添加测试
        bean = readFileStr(dir, path, bean);


        WriteTestFile(writeFiePath, bean, ftlPath, ftlName);
//        String[] daoTemNames = {"base.ftl", "add.ftl", "del.ftl", "update.ftl"};
//        String[] daoFile = {
//                subMoDaoPath + "/" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Add" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Delete" + bean.getJavaFlieName() + "Dao.java",
//                subMoDaoPath + "/Update" + bean.getJavaFlieName() + "Dao.java",
//        };
//        // 生成目标文件
//        String daoPath = bean.getTemplateDirectory() + "/srv/impl/dao";
//        WriteTestFile(daoFile[i], bean, daoPath, daoTemNames[i]);

    }

    public static CoverageBean readFileStr(File file, String dirStr, CoverageBean bean) {

        bean.setMethods(new ArrayList<>());
        bean.setImportDao(new ArrayList<>());
        bean.setCtrlMethods(new ArrayList<>());
        bean.setSerMethods(new ArrayList<>());
        bean.setImportSer(new ArrayList<>());
        bean.setWantCheckIdsForBizData("");
//        System.out.println("===============================================");
        try {
//		String fileName = file.getName();
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());

            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");

            BufferedReader br = new BufferedReader(isr);

            String line = null;
            boolean connectionFlag = false;
            boolean autowiredFlag = false;
            boolean wantsStrFlag = false;

            boolean isvoidMethod = false;

            String wantsStr = "";
            String connectionStr = "";

            String nextStatus = "00";
            if (file.getName().endsWith("Dao.java")) {
                bean.setFileType("Dao");
            } else if (file.getName().endsWith("Service.java")) {
//                                            nextStatus = "99";
                bean.setFileType("Service");
            } else if (file.getName().endsWith("Ctrl.java")) {
//                                            nextStatus = "99";
                bean.setFileType("Ctrl");
            }
            while ((line = br.readLine()) != null) {
                //service 添加方法return的获取

                if (line.trim().contains("@Autowired")) {
                    autowiredFlag = true;
                }
                if (line.trim().indexOf("String[] wantCheckIdsForBizData") >= 0) {
                    wantsStrFlag = true;
//                    System.out.println("--------wantCheckIdsForBizData-------------");
                    System.out.println(line.trim());
                }
                if (line.trim().indexOf("String loggerName") >= 0) {
                    String cvalName = line.substring(line.indexOf("=") + 1, line.indexOf("."));
                    cvalName = cvalName.trim();
                    bean.setCvalName(cvalName);
//                                    System.out.println("---------------------");
                }

                if (wantsStrFlag) {
                    if (line.trim().length() > 0) {
                        String wants = "";
                        if (line.trim().indexOf("{") >= 0) {
                            if (!line.trim().contains("}")) {
                                String str = line.substring(line.indexOf("{") + 1, line.length());
//                                System.out.println(line);
//                                System.out.println(str);
                                wants = bean.getWantCheckIdsForBizData() + str ;

                            } else {
                                wants = bean.getWantCheckIdsForBizData() + line.substring(line.indexOf("{") + 1, line.indexOf("}"));
                                wantsStrFlag = false;
                            }
//                            System.out.println("0:"+bean.getWantCheckIdsForBizData());
//                            System.out.println("1:"+wants);
                        } else {
                            if (!line.trim().contains("}")) {
                                wants = bean.getWantCheckIdsForBizData() + line;

                            } else {
                                wants = bean.getWantCheckIdsForBizData() + line.substring(0, line.indexOf("}") );
                                wantsStrFlag = false;
                            }
//                            System.out.println("1:"+wants);
                        }
//                       System.out.println("3:"+wants);
                        wants = wants.trim();
                        wants = wants.replaceAll("\\s", "");
                        wants = wants.replaceAll("\n", "");
//                        System.out.println(wants);
                        bean.setWantCheckIdsForBizData(wants);
                    }
                    if (wantsStrFlag && line.trim().contains("}")) {
                        wantsStrFlag = false;
//                        System.out.println("---------------------");
                    }
                }

                if (line.trim().indexOf("@Autowired") == -1 && autowiredFlag
                        && line.trim().indexOf("StdB") == -1
                        && line.trim().indexOf("IdSeq") == -1
                        && line.trim().indexOf("LogRed") == -1
                        && line.trim().indexOf("*") == -1
                        && line.trim().indexOf("BsmCon") == -1
                        && line.trim().indexOf("static") == -1
                ) {
                    if ("Service".equals(bean.getFileType())) {
                        List<Map<String, String>> mapList = bean.getImportDao();
                        Map<String, String> map = new HashMap<>();
                        System.out.println("---------------------");
                        System.out.println(line);
                        if (line.length() > 0) {
                            line = trimLeft(line);
                            line = line.replace(";", "");
                            map.put("desc", line);
                            String[] arr = line.split(" ");
                            String name = arr[1].trim();
//                            System.out.println(name);
                            for(int i=1;i<arr.length;i++){
                                if(" ".equals(name)||name.length()==0||"private".equals(name)||"public".equals(name)){
                                    name = arr[i].trim();
                                }else{
                                    break;
                                }
                            }
                            String  name2 = arr[2].trim();
                            for(int i=1;i<arr.length;i++){
                                if(" ".equals(name2)||name2.length()==0||"private".equals(name2)||"public".equals(name2)||name.equals(name2)){
                                    name2 = arr[i].trim();
                                }else{
                                    break;
                                }
                            }
                            map.put("name", name);
                            map.put("name2", name2);
                            mapList.add(map);
                            bean.setImportDao(mapList);
                        }

                    } else  if ("Ctrl".equals(bean.getFileType())) {
                        if (line.trim().indexOf("AppSecre") == -1
                                &&line.trim().indexOf("*") == -1
                                &&line.trim().indexOf("static") == -1
                                &&line.trim().indexOf("public") == -1
                                &&line.trim().indexOf("BsmCon") == -1
                                && line.trim().indexOf("OnsPr") == -1 && line.trim().indexOf("StdBizExt") == -1) {
                            List<Map<String, String>> mapList = bean.getImportSer();
                            Map<String, String> map = new HashMap<>();
//                            System.out.println("---------------------");
//                            System.out.println(line);
                            if (line.length() > 0) {
//                                System.out.println(line);
                                line = trimLeft(line);
                                line = line.replace(";", "");
                                String[] arr = line.split(" ");
                                if(arr.length>2){
                                    map.put("desc", line);
                                    System.out.println(line);

                                    String name = arr[1].trim();
//                                    System.out.println(name);
                                    for(int i=1;i<arr.length;i++){
                                        if(" ".equals(name)||name.length()==0||"private".equals(name)||"public".equals(name)){
                                            name = arr[i].trim();
                                        }else{
                                            break;
                                        }
                                    }
                                String  name2 = arr[2].trim();
                                    for(int i=1;i<arr.length;i++){
                                        if(" ".equals(name2)||name2.length()==0||"private".equals(name2)||"public".equals(name2)||name.equals(name2)){
                                            name2 = arr[i].trim();
                                        }else{
                                            break;
                                        }
                                    }

                                    map.put("name", name);
                                    map.put("name2", name2);
                                    mapList.add(map);
                                    bean.setImportSer(mapList);
                                }

                            }
                        }

                    }

                    autowiredFlag = false;
                }

                if (line.trim().matches(".*public.*") && line.trim().indexOf(";") < 0 && "99".equals(nextStatus)) {
                    nextStatus = "01";
                }

                String curStatus = nextStatus;
                if (line.trim().startsWith("//")) {
                    continue;
                }
                switch (curStatus) {
                    case "00":
                        String s1 = "";

//				import com.flux.wmsv5.a05sysbiz.a0507.impl.dao.WarehouseInfoDao;
                        if (line.trim().matches(".*import.*com.flux.wmsv5.*;.*")) {
//                                packageArray.add(line);
                        }

                        if (line.trim().matches(".*package.*com.flux.wmsv5.*;.*")) {
//                                classObj.packageObj = line.trim();
                        }

                        if (line.trim().matches(".*public.*class.*")) {
                            String strArr = line.trim();
                            for (int i = 0; i < strArr.length(); i++) {
                                char str = strArr.charAt(i);
                                String s = String.valueOf(str); //效率最高的方法
                                if (s.matches("[a-zA-Z0-9]")) {
                                    s1 += s;
                                } else {
                                    if (!"public".equals(s1) && !"class".equals(s1)) {
//                                            file.getName() = s1;
//                                            System.out.println(s1);
                                        s1 = "";
//                                        System.out.println("file.getName():" + file.getName());
                                        nextStatus = "01";


                                        break;
                                    }
                                    s1 = "";
                                }
                            }
                        }
                        break;
                    case "01":
                        if (connectionFlag) {
                            connectionStr += " " + line.trim();
                            if (connectionStr.trim().matches(".*public.*\\(.*\\).*\\{.*")) {
                                connectionFlag = false;
                                dealMethod(connectionStr, bean);
//                                    methodObjList.add(methodObj);
//                                System.out.println("connectionStr:" + connectionStr);
//						System.out.println(methodObj);
                            }
                        }
                        if (line.trim().matches(".*public.*\\(.*") && line.trim().indexOf(";") < 0) {
                            connectionStr = line.trim();
                            connectionFlag = true;
                            if (connectionStr.trim().matches(".*public.*\\(.*\\).*\\{.*")) {
                                connectionFlag = false;
//                                    MethodObj methodObj = new MethodObj();
//                                    dealMethod(connectionStr,methodObj);
//                                    methodObjList.add(methodObj);
                                dealMethod(connectionStr, bean);
//                                System.out.println("connectionStr2:" + connectionStr);
                            }
                        }

//                        if (file.getName().endsWith("Service.java")) {
////					return WarehouseInfoDao.getWarehouseInfo(loginInfoObj, bizDataMap);
//                            if (line.trim().matches(".*return.*\\(.*\\).*;.*")) {
//                                String strArr = line.trim();
//                                String s2 = "";
//                                for (int i = 0; i < strArr.length(); i++) {
//                                    char str = strArr.charAt(i);
//                                    String s = String.valueOf(str); //效率最高的方法
//                                    if (s.matches("[a-zA-Z0-9]")) {
//                                        s2 += s;
////                                            System.out.println(s2);
//                                    } else {
////                                            if(".".equals(s)){
////                                                methodObjList.get(methodObjList.size()-1).contentClassName = s2;
////                                            }else if("(".equals(s)){
////                                                methodObjList.get(methodObjList.size()-1).contentMethodName = s2;
////                                                break;
////                                            }
////                                            System.out.println(s2);
//                                        s2 = "";
//                                    }
//                                }
//                            }
//                        }

//				lastStr = line;
                        break;
//                    case "99":
////                            if(autowiredFlag){
////                                AutowiredObj autowiredObj = new AutowiredObj();
//////                                autowiredObj.entityClass = line.trim().split("\\s|;")[1];
//////                                autowiredObj.entityClassName = line.trim().split("\\s|;")[2];
//////                                autowiredObjList.add(autowiredObj);
//////                                autowiredFlag = false;
//////                                System.out.println(autowiredObj);
////                            }
////                            if(line.trim().indexOf("@Autowired") >= 0){
////                                autowiredFlag = true;
////                            }
////				lastStr = line;
//                        break;
                    default:

                }


                if ("Service".equals(bean.getFileType())) {

                    if (isMethodFlag) {
//                        if(isvoidMethod&&!line.trim().contains("}")){
//
//                        }
                        if (line.trim().contains("return ")||(isvoidMethod&&!line.trim().contains("{"))) {

                            List<Method> methodList = bean.getSerMethods();
                            if (methodList.size() > 0) {

                                String lineStr = line.trim();
//                                System.out.println("lineStr:::"+lineStr);
                                lineStr = lineStr.replace("return ", "");
                                lineStr = lineStr.replace(";", "");
                                lineStr = lineStr.replace(".", ").");
//                                  lineStr = lineStr.replace(".",").");
                                String tmpStr = lineStr;
//                                System.out.println(lineStr);
                                String[] tmpArr = lineStr.split(",");
                                lineStr = lineStr.replace("loginInfoObj", "any(LoginInfoObj.class)");
                                lineStr = lineStr.replace("bizDataMap", "any(Map.class)");

                                for (int i = 0; i < tmpArr.length; i++) {
                                    if (tmpArr[i].contains("Map")) {
                                        lineStr = lineStr.replace(tmpArr[i], "any(Map.class)");
                                    } else if (tmpArr[i].contains("is")) {
                                        lineStr = lineStr.replace(tmpArr[i], "any(Boolean.class)");
                                    }else if (tmpArr[i].contains("arr")||tmpArr[i].contains("Arr")) {
                                        lineStr = lineStr.replace(tmpArr[i], "any(String[].class)");
                                    } else {
                                        lineStr = lineStr.replace(tmpArr[i], "any(String.class)");
                                    }
                                    if (i == tmpArr.length - 1&&tmpArr.length>2) {
                                        lineStr += ")";

                                    }
                                }
                                lineStr += ";";
                                //RulDisZoneDao).getRulDisZoneArea(any(LoginInfoObj.class),any(Map.class));
                                //RulDisZoneDao.getRulDisZoneInfo(loginInfoObj, bizDataMap,sisis)
//                                System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
//                                System.out.println(lineStr);
                                Method method = methodList.get(methodList.size() - 1);
                                method.setmReturnDesc(lineStr);
                                methodList.set(methodList.size() - 1, method);
                                bean.setSerMethods(methodList);
                            }

                            isMethodFlag = false;
                            isvoidMethod = false;
                        }
                        else {
                            isMethodFlag = true;

                        }
                    }
                    if (line.trim().matches(".*public.*\\(.*")&&!line.trim().contains("postData")) {
//                        if (isMethodFlag) {
//                            isMethodFlag = false;
//                        } else {
                            isMethodFlag = true;
                           if(line.trim().contains(" void")){
                               isvoidMethod = true;
                           }
//                        }

                    }
                }

            }
            br.close();


//=======================================================

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("更新错误，文件：" + file.getName() + " 路径：" + file.getAbsolutePath());
        }


        return bean;
    }

    //
    public static void dealMethod(String strArr, CoverageBean bean) {

//        System.out.println("获取方法:" + strArr);
//        strArr = strArr.replaceAll(", ","(");
        Method tmpMethod = new Method();
        String strArr1 = strArr;
        String paramArr1 = strArr.substring(strArr.indexOf("(") + 1, strArr.indexOf(")"));
        strArr1 = strArr1.substring(0, strArr.indexOf("("));
        strArr1 = strArr1.replace(", ", ",");
        strArr1 = strArr1.replace(" ,", ",");
//        System.out.println("获取字符:" + strArr1);
        tmpMethod.setmDesc("//" + strArr1);
        String[] arr = strArr1.split(" ");
        List<String> params = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            String str = arr[i].trim();
//            System.out.println(str);
            if (i == 0) {
                tmpMethod.setmType(str);
            } else if (i == 1) {
                tmpMethod.setmRetrun(str);
            } else if (i == 2) {
                tmpMethod.setmName(str);
            }
        }
//        System.out.println("获取参数:" + paramArr1);
        paramArr1 = paramArr1.replace(", ", ",");
        paramArr1 = paramArr1.replace(" ,", ",");
        String[] arr2 = paramArr1.split(",");
        for (String str : arr2) {
            str = str.trim();
            if (str.contains(">")) {
//                System.out.println("line:"+str);
                String tStr = params.get(params.size() - 1);
                ;
//                System.out.println("from:"+tStr);
                if (str.startsWith("Object")) {
                    tStr = "MapObj";
                } else {
                    tStr = "MapStr";
                }
//                System.out.println("to:"+tStr);
                params.set(params.size() - 1, tStr);
//                continue;
            } else {
                if (!str.startsWith("Map")) {
                    String[] tmArr = str.split(" ");
                    params.add(tmArr[0]);

//                System.out.println(str+"===>"+tmArr[0]);
                } else {
                    params.add("MapObj");
//                System.out.println(str+"===>Map");
                }
            }

        }
//        System.out.println(params.toArray());
        tmpMethod.setmParams(params);
        if ("Dao".equals(bean.getFileType())) {
            List<Method> tempMethods = bean.getMethods();
            tempMethods.add(tmpMethod);
            bean.setMethods(tempMethods);
        } else if ("Service".equals(bean.getFileType())) {
            List<Method> tempMethods = bean.getSerMethods();
            tempMethods.add(tmpMethod);
            bean.setSerMethods(tempMethods);
        } else {
            List<Method> tempMethods = bean.getCtrlMethods();
            tempMethods.add(tmpMethod);
            bean.setCtrlMethods(tempMethods);
        }
//        List<Method> tempMethods = bean.getMethods();
//        tempMethods.add(tmpMethod);
//        bean.setMethods(tempMethods);
    }
//        List<ReturnObj> paraObjList = new ArrayList<ReturnObj>();
//        ReturnObj returnParaObj = new ReturnObj();
//        boolean paraObj = true;
//
//        ReturnObj returnObj = new ReturnObj();
//        test.returnObj = returnObj;
//        test.paraObjList = paraObjList;
//        int lockNum = 0;
////		String lastS1 = "";
//        String s1 = "";
//        String nextStatus2 = "00";
////		String public1 = "";
//        for (int i = 0; i < strArr.length(); i++) {
//            char str = strArr.charAt(i);
//            String s = String.valueOf(str); //效率最高的方法
//            String curStatus2 = nextStatus2;
//            switch (curStatus2) {
//                case "00":
//                    if (s.matches("[a-zA-Z]")) {
//                        s1 += s;
//                    } else {
//                        if ("public".equals(s1)) {
////								public1 = "public";
//                            s1 = "";
//                            nextStatus2 = "01";
//                            test.modified = "public";
//                        }
//                    }
//                    break;
//                case "01"://public
//                    if (s.matches("[a-zA-Z]")) {
//                        s1 += s;
//                    } else {
////							lastS1 = s1;
////							System.out.println(s1);
//                        if ("<".equals(s) || "[".equals(s)) {
//                            lockNum = lockNum + 1;
//                            if ("[".equals(s)) {
//                                test.returnObj.ifArray = true;
//                            }
//                        } else if (">".equals(s) || "]".equals(s)) {
//                            lockNum = lockNum - 1;
//                        }
//
//                        if ("Map".equals(s1)) {
//                            MapObj mapObj = new MapObj();
//                            if ("List".equals(test.returnObj.objType)) {
//                                test.returnObj.listObj.objType = "Map";
//                                test.returnObj.listObj.mapObj = mapObj;
//                            } else {
//                                test.returnObj.objType = "Map";
//                                test.returnObj.mapObj = mapObj;
//                            }
//                            s1 = "";
//                        } else if ("List".equals(s1)) {
//                            ListObj listObj = new ListObj();
//                            test.returnObj.objType = "List";
//                            test.returnObj.listObj = listObj;
//                            s1 = "";
//                        } else if (",".equals(s)) {
//                            if ("Map".equals(test.returnObj.objType)) {
//                                test.returnObj.mapObj.keyStr = s1;
//                                s1 = "";
//                            } else if ("List".equals(test.returnObj.objType)) {
//                                test.returnObj.listObj.mapObj.keyStr = s1;
//                                s1 = "";
//                            }
//                        } else if (">".equals(s)) {
//                            if ("Map".equals(test.returnObj.objType)) {
//                                test.returnObj.mapObj.valueStr = s1;
//                                s1 = "";
//                            } else if ("List".equals(test.returnObj.objType)) {
//                                test.returnObj.listObj.mapObj.valueStr = s1;
//                                s1 = "";
//                            }
//                        } else {
//                            if (Util.isNotNull(s1)) {
//                                test.returnObj.objType = s1;
//                                if ("[".equals(s)) {
//                                    test.returnObj.ifArray = true;
//                                }
//                            }
//                            s1 = "";
//                        }
//                        if (lockNum == 0) {
//                            nextStatus2 = "02";
//                        }
//                    }
//                    break;
//                case "02":
//                    if (s.matches("[a-zA-Z0-9_]")) {//方法名，支持_
//                        s1 += s;
//                    } else {
//                        if (Util.isNotNull(s1)) {
//                            test.methodNameObj = s1;
//                            s1 = "";
//                            nextStatus2 = "03";
//                        }
//                    }
//                    break;
//                case "03":
//
//                    if (paraObj) {
//                        if (s.matches("[a-zA-Z0-9]")) {
//                            s1 += s;
//                        } else {
////								lastS1 = s1;
////								System.out.println(s1);
//                            if ("<".equals(s) || "[".equals(s)) {
//                                lockNum = lockNum + 1;
//                            } else if (">".equals(s) || "]".equals(s)) {
//                                lockNum = lockNum - 1;
//                            }
//
//                            if ("Map".equals(s1)) {
//                                MapObj mapObj = new MapObj();
//                                if ("List".equals(returnParaObj.objType)) {
//                                    returnParaObj.listObj.objType = "Map";
//                                    returnParaObj.listObj.mapObj = mapObj;
//                                } else {
//                                    returnParaObj.objType = "Map";
//                                    returnParaObj.mapObj = mapObj;
//                                }
//                                s1 = "";
//                            } else if ("List".equals(s1)) {
//                                ListObj listObj = new ListObj();
//                                returnParaObj.objType = "List";
//                                returnParaObj.listObj = listObj;
//                                s1 = "";
//                            } else if (",".equals(s)) {
//                                if ("Map".equals(returnParaObj.objType)) {
//                                    returnParaObj.mapObj.keyStr = s1;
//                                    s1 = "";
//                                } else if ("List".equals(returnParaObj.objType)) {
//                                    returnParaObj.listObj.mapObj.keyStr = s1;
//                                    s1 = "";
//                                }
//                            } else if (">".equals(s)) {
//                                if ("Map".equals(returnParaObj.objType)) {
//                                    returnParaObj.mapObj.valueStr = s1;
//                                    s1 = "";
//                                } else if ("List".equals(returnParaObj.objType)) {
//                                    if (Util.isNotNull(s1)) {
//                                        returnParaObj.listObj.mapObj.valueStr = s1;
//                                    }
//                                    s1 = "";
//                                }
//                            } else {
//                                if (Util.isNotNull(s1)) {
//                                    returnParaObj.objType = s1;
//                                    if ("[".equals(s)) {
//                                        returnParaObj.ifArray = true;
//                                    }
//                                }
//                                s1 = "";
//                            }
//                            if (lockNum == 0 && Util.isNotNull(returnParaObj.objType)) {
//                                paraObj = false;
//                            }
//                        }
//                    } else {
//                        if (s.matches("[a-zA-Z0-9_]")) {
//                            s1 += s;
//                        } else {
//                            if (Util.isNotNull(s1)) {
//                                returnParaObj.paraObj = s1;
//                                paraObjList.add(returnParaObj);
//                                returnParaObj = new ReturnObj();
//                                paraObj = true;
//                                s1 = "";
//                            }
//                        }
//                    }
//                    if (s.endsWith(")")) {
//                        nextStatus2 = "04";
//                    }
//                    break;
//                case "04":
//                    if (s.matches("[a-zA-Z0-9]")) {
//                        s1 += s;
//                    } else {
//                        if (Util.isNotNull(s1)) {
//                            test.exceptionObj = s1;
//                            s1 = "";
//                            nextStatus2 = "05";
//                        }
//                    }
//                    break;
//                case "05":
//                    if (s.matches("[a-zA-Z0-9]")) {
//                        s1 += s;
//                    } else {
//                        if (Util.isNotNull(s1)) {
//                            test.exceptionObj = s1;
//                            s1 = "";
//                            nextStatus2 = "05";
//                        }
//                    }
//                    break;
//                default:
//            }
//
//
//        }
//    }

    /**
     * 创建API目录代码
     *
     * @param bean
     * @throws Exception
     */
    public static void createApiCode(AutoBean bean) throws Exception {

        // 模板生成后存放目录
        String targetPath = bean.getAutoPath();
        //创建输入模块主目录
        targetPath = targetPath + "/t" + bean.getNumMain() + bean.getNumSub() + "代码";
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/api";
        CreateFilePath(apiMk);
        String apiFileName = bean.getJavaFlieName() + "Service.java";
        String apiTemName = "api.ftl";
        String apiFile = apiMk + '/' + apiFileName;
//        System.out.println(apiFile);

        // 生成目标文件
        String path = bean.getTemplateDirectory() + "/api";
        WriteFile(apiFile, bean, path, apiTemName);

        if (bean.getDetailTableList() != null && bean.getDetailTableList().size() > 0) {
            for (int i = 0; i < bean.getDetailTableList().size(); i++) {
                //创建Api
                String apiDetailFileName = bean.getDetailTableList().get(i).getJavaFlieName() + "Service.java";
                String apiDetailTemName = "apiDetail.ftl";
                String apiDetailFile = apiMk + '/' + apiDetailFileName;
                System.out.println(apiDetailFile);

                // 生成目标文件
                String pathDetail = bean.getTemplateDirectory() + "/api";
                WriteDetailFile(apiDetailFile, bean, i, pathDetail, apiDetailTemName);
            }
        }
    }


    /**
     * 创建 Srv 目录代码
     *
     * @param
     */
    public static void createSrvCode(AutoBean bean) throws Exception {

        //**********************************生成目录**********************************************************//
        // 模板生成后存放目录
//        String targetPath = bean.getAutoPath();
        String targetPath = bean.getAutoPath();
        //创建输入模块主目录
        targetPath = targetPath + "/t" + bean.getNumMain() + bean.getNumSub() + "代码";
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/srv";
        CreateFilePath(apiMk);
        //生成子模块目录 /t0101
//        String subMoPath = apiMk + "/t" + bean.getNumMain() + bean.getNumSub();
        String subMoPath = apiMk + "/t" + bean.getNumMain() + bean.getNumSub();
        String subMoImplPath = subMoPath + "/impl";
        String subMoCtrlPath = subMoImplPath + "/ctrl";
        String subMoDaoPath = subMoImplPath + "/dao";
        String subMoServPath = subMoImplPath + "/service";

        CreateFilePath(subMoPath);
        CreateFilePath(subMoImplPath);
        CreateFilePath(subMoCtrlPath);
        CreateFilePath(subMoDaoPath);
        CreateFilePath(subMoServPath);

        //************************************生成CVal.java********************************************************//
        //创建BaseAreaCVal.java
        String apiFileName = bean.getJavaFlieName() + "CVal.java";
//        String apiFileName = bean.getJavaFlieName() + "CVal.java";
        String apiTemName = "cval.ftl";
        String apiFile = subMoPath + '/' + apiFileName;
        System.out.println(apiFile);

        // 生成目标文件
        String path = bean.getTemplateDirectory() + "/srv";
//        String path = bean.getTemplateDirectory() + "/srv";
        WriteFile(apiFile, bean, path, apiTemName);

        //************************************生成Impl.java********************************************************//
        //创建 BaseAreaServiceImpl.java
        String impFileName = bean.getJavaFlieName() + "ServiceImpl.java";
        String implTemName = "impl.ftl";
        String implFile = subMoImplPath + '/' + impFileName;
        System.out.println(implFile);

        // 生成目标文件
        String implPath = bean.getTemplateDirectory() + "/srv/impl";
        WriteFile(implFile, bean, implPath, implTemName);

        //*************************************生成Dao 4文件*******************************************************//

        //创建 BaseAreaServiceImpl.java
//        String daoFileName = getKey("JAVA_HEADER + "ServiceImpl.java";
        String[] daoTemNames = {"base.ftl", "add.ftl", "del.ftl", "update.ftl"};
        String[] daoFile = {
                subMoDaoPath + "/" + bean.getJavaFlieName() + "Dao.java",
                subMoDaoPath + "/Add" + bean.getJavaFlieName() + "Dao.java",
                subMoDaoPath + "/Delete" + bean.getJavaFlieName() + "Dao.java",
                subMoDaoPath + "/Update" + bean.getJavaFlieName() + "Dao.java",
        };
        // 生成目标文件
        String daoPath = bean.getTemplateDirectory() + "/srv/impl/dao";
        for (int i = 0; i < daoTemNames.length; i++) {
            System.out.println(daoFile[i]);
            WriteFile(daoFile[i], bean, daoPath, daoTemNames[i]);
        }

        //*************************************生成Service 4文件*******************************************************//

        //创建 BaseAreaServiceImpl.java
//        String serFileName = getKey("JAVA_HEADER + "ServiceImpl.java";
        String[] serTemNames = {"add.ftl", "del.ftl", "update.ftl"};
        String[] serFile = {
//                subMoServPath + "/" + getKey("JAVA_HEADER + "Service.java",
                subMoServPath + "/Add" + bean.getJavaFlieName() + "Service.java",
                subMoServPath + "/Delete" + bean.getJavaFlieName() + "Service.java",
                subMoServPath + "/Update" + bean.getJavaFlieName() + "Service.java",
        };
        // 生成目标文件
        String serPath = bean.getTemplateDirectory() + "/srv/impl/service";
        for (int i = 0; i < serTemNames.length; i++) {
            System.out.println(daoFile[i]);
            WriteFile(serFile[i], bean, serPath, serTemNames[i]);
        }

        //*************************************生成Ctrl 4文件*******************************************************//

        //创建 BaseAreaServiceImpl.java
//        String ctrFileName = getKey("JAVA_HEADER + "ServiceImpl.java";
        String[] ctrTemNames = {"add.ftl", "del.ftl", "update.ftl"};
        String[] ctrFile = {
//                subMoCtrlPath + "/" + getKey("JAVA_HEADER + "Ctrl.java",
                subMoCtrlPath + "/Add" + bean.getJavaFlieName() + "ServiceCtrl.java",
                subMoCtrlPath + "/Delete" + bean.getJavaFlieName() + "ServiceCtrl.java",
                subMoCtrlPath + "/Update" + bean.getJavaFlieName() + "ServiceCtrl.java",
        };
        // 生成目标文件
        String ctrPath = bean.getTemplateDirectory() + "/srv/impl/ctrl";
        for (int i = 0; i < serTemNames.length; i++) {
            System.out.println(daoFile[i]);
            WriteFile(ctrFile[i], bean, ctrPath, ctrTemNames[i]);
        }

        //*************************************生成xml 1文件*******************************************************//

//        //创建 BaseAreaServiceImpl.java
//        String xmlFileName = "xml.txt";
//        String xmllTemName = "srv_xml.ftl";
//        String xmlFile = apiMk + '/' + xmlFileName;
//        System.out.println(implFile);
//
//        // 生成目标文件
//        String xmllPath = bean.getTemplateDirectory() + "/srv/xml";
////       WriteDetailFile(xmlFile, bean,0, xmllPath, xmllTemName);

        //*********************************************************************************************************//
    }

    /**
     * 创建 Srv  详情目录代码
     *
     * @param
     */
    public static void createSrvDetailCode(AutoBean bean) throws Exception {
        if (bean.getDetailTableList() != null && bean.getDetailTableList().size() > 0) {
            for (int k = 0; k < bean.getDetailTableList().size(); k++) {
                //**********************************生成目录**********************************************************//
                // 模板生成后存放目录
                String targetPath = bean.getAutoPath();
                //创建输入模块主目录
                targetPath = targetPath + "/t" + bean.getNumMain() + bean.getNumSub() + "代码";
                // 模板生成后新文件名
                //创建Api
                String apiMk = targetPath + "/srv";
//                CreateFilePath(apiMk);
                //生成子模块目录 /t0101
                //        String subMoPath = apiMk + "/t" + bean.getNumMain() + bean.getNumSub();
                String subMoPath = apiMk + "/t" + bean.getNumMain() + bean.getNumSub();
                String subMoImplPath = subMoPath + "/impl";
                String subMoCtrlPath = subMoImplPath + "/ctrl";
                String subMoDaoPath = subMoImplPath + "/dao";
                String subMoServPath = subMoImplPath + "/service";

//                CreateFilePath(subMoPath);
//                CreateFilePath(subMoImplPath);
//                CreateFilePath(subMoCtrlPath);
//                CreateFilePath(subMoDaoPath);
//                CreateFilePath(subMoServPath);

                //************************************生成CVal.java********************************************************//
                //创建BaseAreaXXXDCVal.java
                String apiFileName = bean.getDetailTableList().get(k).getJavaFlieName() + "CVal.java";
                //        String apiFileName = bean.getJavaFlieName() + "CVal.java";
                String apiTemName = "cvalDetail.ftl";
                String apiFile = subMoPath + '/' + apiFileName;
                System.out.println(apiFile);

                // 生成目标文件
                String path = bean.getTemplateDirectory() + "/srv";
                //        String path = bean.getTemplateDirectory() + "/srv";
                WriteDetailFile(apiFile, bean, k, path, apiTemName);

                //************************************生成Impl.java********************************************************//
                //创建 BaseAreaServiceImpl.java
                String impFileName = bean.getDetailTableList().get(k).getJavaFlieName() + "ServiceImpl.java";
                String implTemName = "implDetail.ftl";
                String implFile = subMoImplPath + '/' + impFileName;
                System.out.println(implFile);

                // 生成目标文件
                String implPath = bean.getTemplateDirectory() + "/srv/impl";
                WriteDetailFile(implFile, bean, k, implPath, implTemName);

                //*************************************生成Dao 4文件*******************************************************//

                //创建 BaseAreaServiceImpl.java
                //        String daoFileName = getKey("JAVA_HEADER + "ServiceImpl.java";
                String[] daoTemNames = {"baseDetail.ftl", "addDetail.ftl", "delDetail.ftl", "updateDetail.ftl"};
                String[] daoFile = {
                        subMoDaoPath + "/" + bean.getDetailTableList().get(k).getJavaFlieName() + "Dao.java",
                        subMoDaoPath + "/Add" + bean.getDetailTableList().get(k).getJavaFlieName() + "Dao.java",
                        subMoDaoPath + "/Delete" + bean.getDetailTableList().get(k).getJavaFlieName() + "Dao.java",
                        subMoDaoPath + "/Update" + bean.getDetailTableList().get(k).getJavaFlieName() + "Dao.java",
                };
                // 生成目标文件
                String daoPath = bean.getTemplateDirectory() + "/srv/impl/dao";
                for (int i = 0; i < daoTemNames.length; i++) {
                    System.out.println(daoFile[i]);
                    WriteDetailFile(daoFile[i], bean, k, daoPath, daoTemNames[i]);
                }

                //*************************************生成Service 4文件*******************************************************//

                //创建 BaseAreaServiceImpl.java
                //        String serFileName = getKey("JAVA_HEADER + "ServiceImpl.java";
                String[] serTemNames = {"addDetail.ftl", "delDetail.ftl", "updateDetail.ftl"};
                String[] serFile = {
                        //                subMoServPath + "/" + getKey("JAVA_HEADER + "Service.java",
                        subMoServPath + "/Add" + bean.getDetailTableList().get(k).getJavaFlieName() + "Service.java",
                        subMoServPath + "/Delete" + bean.getDetailTableList().get(k).getJavaFlieName() + "Service.java",
                        subMoServPath + "/Update" + bean.getDetailTableList().get(k).getJavaFlieName() + "Service.java",
                };
                // 生成目标文件
                String serPath = bean.getTemplateDirectory() + "/srv/impl/service";
                for (int i = 0; i < serTemNames.length; i++) {
                    System.out.println(daoFile[i]);
                    WriteDetailFile(serFile[i], bean, k, serPath, serTemNames[i]);
                }

                //*************************************生成Ctrl 4文件*******************************************************//

                //创建 BaseAreaServiceImpl.java
                //        String ctrFileName = getKey("JAVA_HEADER + "ServiceImpl.java";
                String[] ctrTemNames = {"addDetail.ftl", "delDetail.ftl", "updateDetail.ftl"};
                String[] ctrFile = {
                        //                subMoCtrlPath + "/" + getKey("JAVA_HEADER + "Ctrl.java",
                        subMoCtrlPath + "/Add" + bean.getDetailTableList().get(k).getJavaFlieName() + "ServiceCtrl.java",
                        subMoCtrlPath + "/Delete" + bean.getDetailTableList().get(k).getJavaFlieName() + "ServiceCtrl.java",
                        subMoCtrlPath + "/Update" + bean.getDetailTableList().get(k).getJavaFlieName() + "ServiceCtrl.java",
                };
                // 生成目标文件
                String ctrPath = bean.getTemplateDirectory() + "/srv/impl/ctrl";
                for (int i = 0; i < serTemNames.length; i++) {
                    System.out.println(daoFile[i]);
                    WriteDetailFile(ctrFile[i], bean, k, ctrPath, ctrTemNames[i]);
                }

                //*************************************生成xml 1文件*******************************************************//

//                //创建 BaseAreaServiceImpl.java
//                String xmlFileName = "xml.txt";
//                String xmllTemName = "srv_xml.ftl";
//                String xmlFile = apiMk + '/' + xmlFileName;
//                System.out.println(implFile);
//
//                // 生成目标文件
//                String xmllPath = bean.getTemplateDirectory() + "/srv/xml";
//                WriteFile(xmlFile, bean, xmllPath, xmllTemName);
            }
        }

        //*********************************************************************************************************//
    }

    /**
     * 创建 web 目录代码
     *
     * @param bean
     */
    public static void createWebCode(AutoBean bean) throws Exception {

//**********************************生成目录**********************************************************//
        // 模板生成后存放目录
        String targetPath = bean.getAutoPath();
        //创建输入模块主目录
        targetPath = targetPath + "/t" + bean.getNumMain() + bean.getNumSub() + "代码";
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/web";
        CreateFilePath(apiMk);
        //生成子模块目录 /t0101
        String moPath = "/t" + bean.getNumMain() + bean.getNumSub();
        String subMoPath = apiMk + moPath;
//        String subMoSrclPath = apiMk + "/src";
        String subMoJslPath = apiMk + "/js/t" + bean.getNumMain() + bean.getNumSub();

        String subMoDaoPath = subMoPath + "/dao";
        String subMoServPath = subMoPath + "/service";

        CreateFilePath(subMoPath);
        CreateFilePath(subMoJslPath);
        CreateFilePath(subMoDaoPath);
        CreateFilePath(subMoServPath);

        //************************************生成CVal.java********************************************************//
        //创建BaseAreaCVal.java
        String apiFileName = bean.getJavaFlieName() + "CVal.java";
        String apiTemName = "BaseCVal.ftl";
        String apiFile = subMoPath + '/' + apiFileName;
        System.out.println(apiFile);
        // 生成目标文件
        String path = bean.getTemplateDirectory() + "/web/src";
        WriteFile(apiFile, bean, path, apiTemName);

        //************************************生成Ih.java********************************************************//
        //创建 BaseArea1H.java
        String impFileName = bean.getJavaFlieName() + "1H.java";
        String implTemName = "Base1H.ftl";
        String implFile = subMoPath + '/' + impFileName;
        System.out.println(implFile);

        // 生成目标文件
        String implPath = bean.getTemplateDirectory() + "/web/src";
        WriteFile(implFile, bean, implPath, implTemName);


        //************************************生成Helper.java********************************************************//
        //创建 ImpHeaderFmExcelHelper.java
        String helperFileName = "ImpHeaderFmExcelHelper.java";
        String impTemName = "ImpHeaderFmExcelHelper.ftl";
        String helpFile = subMoPath + '/' + helperFileName;
        System.out.println(helpFile);

        // 生成目标文件
        String helpPath = bean.getTemplateDirectory() + "/web/src";
        WriteFile(helpFile, bean, helpPath, impTemName);

        //*************************************生成Dao 1文件*******************************************************//

        //创建 ImpHeaderFmExcelHelper.java
        String daoFileName = bean.getJavaFlieName() + "Dao.java";
        String daoTemName = "BaseDao.ftl";
        String daoTemFile = subMoDaoPath + '/' + daoFileName;
        System.out.println(daoTemFile);

        // 生成目标文件
        String daoPath = bean.getTemplateDirectory() + "/web/src/dao";
        WriteFile(daoTemFile, bean, daoPath, daoTemName);
//        //*************************************生成Service 1文件*******************************************************//

        //创建 ImpHeaderFmExcelHelper.java
        String serFileName = bean.getJavaFlieName() + "WebService.java";
        String serTemName = "BaseService.ftl";
        String serTemFile = subMoServPath + '/' + serFileName;
        System.out.println(serTemFile);

        // 生成目标文件
        String serPath = bean.getTemplateDirectory() + "/web/src/service";
        WriteFile(serTemFile, bean, serPath, serTemName);
//

//        //*************************************生成xml 1文件*******************************************************//
//
        //创建 BaseAreaServiceImpl.java
        String xmlFileName = "xml.txt";
        String xmllTemName = "web_xml.ftl";
        String xmlFile = apiMk + "/" + xmlFileName;
        System.out.println(implFile);

        // 生成目标文件
        String xmllPath = bean.getTemplateDirectory() + "/web/xml";
        WriteDetailFile(xmlFile, bean, 0, xmllPath, xmllTemName);

        //*************************************生成js  model 1文件*******************************************************//

        //创建 BaseAreaServiceImpl.java
        String modelFileName = CreateUtil.toLowerCaseFirstOne(bean.getJavaFlieName()) + "Model.js";
        String moddelTemName = "baseTableModel.ftl";
        String modellFile = null;

        modellFile = subMoJslPath + "/" + modelFileName;

        System.out.println(modellFile);

//        map.put("modeName",modelFileName);
        String modelPath = null;
        // 生成目标文件

        if (bean.getSubTableName() != null && !bean.getSubTableName().equals("")) {
            modelPath = bean.getTemplateDirectory() + "/web/js/detail";
        } else {
            if ("BaseView".equals(bean.getModelType())) {
                //取用baseView 作为主模板
                modelPath = bean.getTemplateDirectory() + "/web/js/baseView";
            } else {
                //取用其他作为主模板
                modelPath = bean.getTemplateDirectory() + "/web/js/baseView";
            }

        }


        WriteFile(modellFile, bean, modelPath, moddelTemName);

        //*************************************生成js  view 1文件*******************************************************//

        //创建 BaseAreaServiceImpl.java
        String viewFileName = CreateUtil.toLowerCaseFirstOne(bean.getJavaFlieName()) + "View.js";
        String viewTemName = "baseTableView.ftl";
        String viewlFile = null;

        viewlFile = subMoJslPath + "/" + viewFileName;
        System.out.println(viewlFile);

        // 生成目标文件
        String viewPath = null;
        // 生成目标文件
        if (bean.getSubTableName() != null && !bean.getSubTableName().equals("")) {
            viewPath = bean.getTemplateDirectory() + "/web/js/detail";
        } else {
            if ("BaseView".equals(bean.getModelType())) {
                //取用baseView 作为主模板
                viewPath = bean.getTemplateDirectory() + "/web/js/baseView";
            } else {
                //取用其他作为主模板
                modelPath = bean.getTemplateDirectory() + "/web/js/baseView";
            }
        }
        WriteFile(viewlFile, bean, viewPath, viewTemName);

        //******************************************判断是否含有详情********************************************************//
        if (bean.getDetailTableList() != null && bean.getDetailTableList().size() > 0) {
            //************************************生成2D.java*********************************//
            List<AutoBean.DetailTableListBean> detailList = bean.getDetailTableList();
            for (int i = 0; i < bean.getDetailTableList().size(); i++) {
                //创建 BaseArea2D.java
                String impDetailFileName = detailList.get(i).getJavaFlieName() + ".java";
                String impDetailTemName = "Base2-nD.ftl";
                String impDetailFile = subMoPath + '/' + impDetailFileName;
                System.out.println(impDetailFile);

                // 生成目标文件
                String impDetailPath = bean.getTemplateDirectory() + "/web/src";
                WriteDetailFile(impDetailFile, bean, i, impDetailPath, impDetailTemName);

                //************************************生成CVal.java********************************************************//
                //创建BaseAreaCVal.java
                String apiCvalFileName = detailList.get(i).getJavaFlieName() + "CVal.java";
                String apiCvalTemName = "BaseDetailCVal.ftl";
                String apiCvalFile = subMoPath + '/' + apiCvalFileName;
                System.out.println(apiCvalFile);
                // 生成目标文件
                String pathCval = bean.getTemplateDirectory() + "/web/src";
                WriteDetailFile(apiCvalFile, bean, i, pathCval, apiCvalTemName);

                //************************************生成Helper.java*************************//
                //创建 ImpHeaderFmExcelHelper.java
                String helperDetailFileName = "Imp" + detailList.get(i).getSubFuctionId() + "FmExcelHelper.java";
                String impDetailImemName = "ImpDetailFmExcelHelper.ftl";
                String helpDetailFile = subMoPath + '/' + helperDetailFileName;
                System.out.println(helpDetailFile);

                // 生成目标文件
                String helpDetailPath = bean.getTemplateDirectory() + "/web/src";
                WriteDetailFile(helpDetailFile, bean, i, helpDetailPath, impDetailImemName);

                //*************************************生成Dao 1文件*******************************************************//

                //创建 Dao.java
                String daoDetailFileName = detailList.get(i).getJavaFlieName() + "Dao.java";
                String daoDetailTemName = "BaseDetailDao.ftl";
                String daoDetailTemFile = subMoDaoPath + '/' + daoDetailFileName;
                System.out.println(daoDetailTemFile);

                // 生成目标文件
                String daoDetailPath = bean.getTemplateDirectory() + "/web/src/dao";
                WriteDetailFile(daoDetailTemFile, bean, i, daoDetailPath, daoDetailTemName);
                //        //*************************************生成Service 1文件*******************************************************//

                //创建 Service.java
                String serDetailFileName = detailList.get(i).getJavaFlieName() + "Service.java";
                String serDetailTemName = "BaseDetailService.ftl";
                String serDetailTemFile = subMoServPath + '/' + serDetailFileName;
                System.out.println(serDetailTemFile);

                // 生成目标文件
                String serDetailPath = bean.getTemplateDirectory() + "/web/src/service";
                WriteDetailFile(serDetailTemFile, bean, i, serDetailPath, serDetailTemName);
                //
                //*************************************生成xml 1文件*******************************************************//
                // xml 在一起生成，不单独生成

                //*************************************生成js  model 1文件*******************************************************//

                //创建 BaseAreaServiceImpl.java
                String modelDetailFileName = CreateUtil.toLowerCaseFirstOne(detailList.get(i).getJavaFlieName()) + "Model.js";
                String moddelDeailTemName = "baseTableModel.ftl";
                String modellDeaillFile = null;
                modellDeaillFile = subMoJslPath + "/" + modelDetailFileName;
                System.out.println(modellDeaillFile);
                String modelDetailPath = null;
                // 生成目标文件
                modelDetailPath = bean.getTemplateDirectory() + "/web/js/detail";
                WriteDetailFile(modellDeaillFile, bean, i, modelDetailPath, moddelDeailTemName);

                //*************************************生成js  view 1文件*******************************************************//

                //创建 BaseAreaServiceImpl.java
                String viewDeatilFileName = CreateUtil.toLowerCaseFirstOne(detailList.get(i).getJavaFlieName()) + "View.js";
                String viewDetailTemName = "baseTableView.ftl";
                String viewlDetailFile = subMoJslPath + "/" + viewDeatilFileName;
                System.out.println(viewlDetailFile);
                // 生成目标文件
                String viewDetailPath = bean.getTemplateDirectory() + "/web/js/detail";
                WriteDetailFile(viewlDetailFile, bean, i, viewDetailPath, viewDetailTemName);
            }
        }
        //*********************************************************************************************************//


        //*******************************************************************************************************************//

    }


    /**
     * 创建 web 目录代码
     *
     * @param bean
     */
    public static void createJsCode(AutoBean bean) throws Exception {

//**********************************生成目录**********************************************************//
        // 模板生成后存放目录
        String targetPath = bean.getAutoPath();
        //创建输入模块主目录
        targetPath = targetPath + "/t" + bean.getNumMain() + bean.getNumSub() + "代码model";
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/web";
        CreateFilePath(apiMk);
        //生成子模块目录 /t0101
        String moPath = "/t" + bean.getNumMain() + bean.getNumSub();
        String subMoPath = apiMk + moPath;
//        String subMoSrclPath = apiMk + "/src";
        String subMoJslPath = apiMk + "/js/t" + bean.getNumMain() + bean.getNumSub();

        String subMoDaoPath = subMoPath + "/dao";
        String subMoServPath = subMoPath + "/service";

        CreateFilePath(subMoPath);
        CreateFilePath(subMoJslPath);
        CreateFilePath(subMoDaoPath);
        CreateFilePath(subMoServPath);

        //*************************************生成js  model 1文件*******************************************************//

        //创建 BaseAreaServiceImpl.java
        String modelDetailFileName = CreateUtil.toLowerCaseFirstOne(bean.getJavaFlieName()) + "Model.js";
        String moddelDeailTemName = "baseTable2Model.ftl";
        String modellDeaillFile = null;
        modellDeaillFile = subMoJslPath + "/" + modelDetailFileName;
        System.out.println(modellDeaillFile);
        String modelDetailPath = null;
        // 生成目标文件
        modelDetailPath = bean.getTemplateDirectory() + "/web/js/detail";
        WriteFile(modellDeaillFile, bean, modelDetailPath, moddelDeailTemName);

        //*******************************************************************************************************************//

    }


    /**
     *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
     */

    /**
     * 创建目录
     *
     * @param
     */
    private static void CreateFilePath(String apiMk) {
        new File(apiMk).mkdirs();
        File apiFileMk = new File(apiMk);
        if (!apiFileMk.exists()) {
            apiFileMk.mkdir();
            throw new RuntimeException("File \'" + apiMk + "\' already exists");
        }
    }


    private static void WriteFile(String fileName, AutoBean bean, String path, String apiTemName) throws Exception {
        Writer writer = null;
        try {
            writer = new FileWriter(fileName);
            Template template = getConfiguration(path).getTemplate(apiTemName, "UTF-8");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("modelFormList", bean.getModelFormList());
//            map.put("modelBeanStrList", bean);
            map.put("bean", bean);
            template.process(map, writer);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            writer.close();
        }
    }

    private static void WriteTestFile(String fileName, CoverageBean bean, String path, String apiTemName) {
        Writer writer = null;
        try {
            if(fileName==null){
                return;
            }
            writer = new FileWriter(fileName);
            Template template = getConfiguration(path).getTemplate(apiTemName, "UTF-8");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("bean", bean);
            template.process(map, writer);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }


    public static void WriteCsvFile(String fileName, ArrayList<String[]> list, String path, String apiTemName) throws Exception {
        Writer writer = null;
        try {
            writer = new FileWriter(fileName);
            Template template = getConfiguration(path).getTemplate(apiTemName, "UTF-8");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("list", list);
            template.process(map, writer);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            writer.close();
        }
    }

    private static void WriteDetailFile(String fileName, AutoBean bean, int i, String path, String apiTemName) throws Exception {
        Writer writer = null;
        try {
            writer = new FileWriter(fileName);
//            Template template = getConfiguration(path).getTemplate(apiTemName, "UTF-8");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("bean", bean);
            AutoBean.DetailTableListBean detail = null;
            if (bean.getDetailTableList().size() > 0) {
                detail = bean.getDetailTableList().get(i);
                String[] tableListDescArr = (detail.getTableListStr()).split(",");
                List<String> tableList = Arrays.asList(tableListDescArr);
                detail.setTableList(tableList);
            }

            map.put("detail", detail);
            map.put("details", bean.getDetailTableList());
            map.put("num", i);
            map.put("i", i);
            Template template = getConfiguration(path).getTemplate(apiTemName, "UTF-8");
            template.process(map, writer);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            writer.close();
        }
    }

    /**
     * 读取json文件，返回json串
     *
     * @param fileName
     * @return
     */
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);

            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param templateDirectory
     * @return
     */
    private static Configuration getConfiguration(String templateDirectory) {
        Configuration configuration = new Configuration();
        try {
            configuration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
            configuration.setDirectoryForTemplateLoading(new File(templateDirectory));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return configuration;
    }

    //首字母转小写
    public static String toLowerCaseFirstOne(String s) {
        if (Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }


    /**
     * 创建API目录代码
     *
     * @param bean
     * @throws Exception
     */
    public static void createTest01(ArrayList<String[]> bean) throws Exception {

        // 模板生成后存放目录
        String targetPath = "E:\\v6autoOut\\test";
        //创建输入模块主目录
        targetPath = targetPath + "/测试01代码";
        System.out.println(targetPath);
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/test";
        CreateFilePath(apiMk);
        System.out.println(apiMk);
        String apiFileName = "test01" + ".csv";
        String apiTemName = "test01.ftl";
        String apiFile = apiMk + '/' + apiFileName;
        System.out.println(apiFile); //目标文件

        // 生成目标文件
        String path = "V6NewTools/src/main/resources/templates/" + "/test";
        System.out.println(path); //目标文件
        WriteCsvFile(apiFile, bean, path, apiTemName);

    }

    /**
     * 创建API目录代码
     *
     * @param bean
     * @throws Exception
     */
    public static void createTest02(ArrayList<String[]> bean) throws Exception {

        // 模板生成后存放目录
        String targetPath = "E:\\v6autoOut\\test";
        //创建输入模块主目录
        targetPath = targetPath + "/测试01代码";
        System.out.println(targetPath);
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/test";
        CreateFilePath(apiMk);
        System.out.println(apiMk);
        String apiFileName = "action" + ".java";
        String apiTemName = "action.ftl";
        String apiFile = apiMk + '/' + apiFileName;
        System.out.println(apiFile); //目标文件

        // 生成目标文件
        String path = "V6NewTools/src/main/resources/templates/" + "/test";
        System.out.println(path); //目标文件
        WriteCsvFile(apiFile, bean, path, apiTemName);

    }

    /**
     * 创建API目录代码
     *
     * @param bean
     * @throws Exception
     */
    public static void createTestSql01(ArrayList<String[]> bean) throws Exception {

        // 模板生成后存放目录
        String targetPath = "E:\\v6autoOut\\test";
        //创建输入模块主目录
        targetPath = targetPath + "/测试01代码";
//        System.out.println(targetPath);
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/test";
        CreateFilePath(apiMk);
//        System.out.println(apiMk);
        String apiFileName = "BSM_FUNCTION_PAGE" + ".sql";
        String apiTemName = "sql1.ftl";
        String apiFile = apiMk + '/' + apiFileName;
        System.out.println(apiFile); //目标文件

        // 生成目标文件
        String path = "V6NewTools/src/main/resources/templates/" + "/test";
//        System.out.println(path); //目标文件
        WriteCsvFile(apiFile, bean, path, apiTemName);

    }

    /**
     * 创建API目录代码
     *
     * @param bean
     * @throws Exception
     */
    public static void createTestSql02(ArrayList<String[]> bean) throws Exception {

        // 模板生成后存放目录
        String targetPath = "E:\\v6autoOut\\test";
        //创建输入模块主目录
        targetPath = targetPath + "/测试01代码";
//        System.out.println(targetPath);
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/test";
        CreateFilePath(apiMk);
//        System.out.println(apiMk);
        String apiFileName = "BSM_FUNCTION_PAGE_ML" + ".sql";
        String apiTemName = "sql2.ftl";
        String apiFile = apiMk + '/' + apiFileName;
        System.out.println(apiFile); //目标文件

        // 生成目标文件
        String path = "V6NewTools/src/main/resources/templates/" + "/test";
//        System.out.println(path); //目标文件
        WriteCsvFile(apiFile, bean, path, apiTemName);

    }

    /**
     * 创建API目录代码
     *
     * @param bean
     * @throws Exception
     */
    public static void createTest03(ArrayList<String[]> bean) throws Exception {

        // 模板生成后存放目录
        String targetPath = "E:\\v6autoOut\\test";
        //创建输入模块主目录
        targetPath = targetPath + "/测试01代码";
        System.out.println(targetPath);
        // 模板生成后新文件名
        //创建Api
        String apiMk = targetPath + "/test";
        CreateFilePath(apiMk);
        System.out.println(apiMk);
        String apiFileName = "test02CVal" + ".java";
        String apiTemName = "cval.ftl";
        String apiFile = apiMk + '/' + apiFileName;
        System.out.println(apiFile); //目标文件

        // 生成目标文件
        String path = "V6NewTools/src/main/resources/templates/" + "/test";
        System.out.println(path); //目标文件
        WriteCsvFile(apiFile, bean, path, apiTemName);

    }

    /**
     * 去左空格
     *
     * @param str
     * @return
     */
    public static String trimLeft(String str) {

        for (int i = 0; i < str.length(); i++) {
            char charStr = str.charAt(i);
            String s = String.valueOf(charStr);
            if ("".equals(s)) {
                str = str.substring(1, str.length());
                return trimLeft(str);
            } else {
                return str;
            }
        }

        return str;
    }

//
//    public static  String getKey(String keyNmae){
//        //config为属性文件名，放在包com.test.config下，如果是放在src下，直接用config即可
//
//
//        try {
////            Properties properties = new Properties();
////            // 使用InPutStream流读取properties文件
////            BufferedReader bufferedReader = null;
////            bufferedReader = new BufferedReader(new FileReader("E:\\Eason\\work\\autoCode2\\src\\config.properties"));
////            properties.load(bufferedReader);
//
//            Properties props = new Properties();
//            FileInputStream in2 = new FileInputStream(System.getProperty("user.dir")+"/conf/config.properties");
//
//            System.out.println(System.getProperty("user.dir"));
////            PropertyConfigurator.configure(System.getProperty("user.dir") + "/config/log4j.properties");
////            logger.debug("Path: " + System.getProperty("user.dir"));
//            InputStreamReader in = new InputStreamReader(in2,"UTF-8");
//            props.load(in);
//
////            fromDB = props.getProperty("fromDB");
//            return props.getProperty(keyNmae);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
////
////             // 获取key对应的value值
////
////        ResourceBundle resource = ResourceBundle.getBundle("config");
////
////         resource.getString(keyNmae);
//    }
}
